/**
 * ...
 * @author Thot
 */

function MapLayersMediator( p_view )
{
	this.init( MapLayersMediator.NAME, p_view );
}

POO.extend( MapLayersMediator, Mediator );


	// static
	MapLayersMediator.NAME = "MapLayersMediator";
	
	
	//private
	MapLayersMediator.prototype._screen = null;
	
	// public
	MapLayersMediator.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [p_name, p_data] );
		this._screen = new MapLayersScreen();
		this.listenNotifications();	
		this.initMapList();
	};
	
	MapLayersMediator.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.ADD_MAP, 				this.onNotification );
		this.addNotificationListener( ApplicationNotification.SELECT_MAP, 			this.onNotification );
		this.addNotificationListener( ApplicationNotification.DISPLAY_MAPS, 		this.onNotification );
		this.addNotificationListener( ApplicationNotification.REMOVE_MAP, 			this.onNotification );
	};

	MapLayersMediator.prototype.onNotification = function( p_notificationType, p_data )
	{
		var proxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var mapProxy = this.getFacade().getProxy( MapProxy.NAME );
		var i = 0;
		var max = 0;
		var vos = null;
		
		switch ( p_notificationType )
		{
				
			case ApplicationNotification.ADD_MAP:
				this._screen.addMap( p_data );
				break;
				
			// useless already did on selectmapcommand
			case ApplicationNotification.SELECT_MAP:
				proxy.currentMap = p_data;
				break;

			case ApplicationNotification.REMOVE_MAP:
				this._screen.removeMap( proxy.currentMap );
				break;
				
			case ApplicationNotification.DISPLAY_MAPS:
				//this._screen.removeMap( proxy.currentMap );
				
				
				vos = mapProxy.getData();
				max = vos.length;
				
				for( i = 0; i < max; i++ )
				{
					this._screen.addMap( vos[i].name );
				}
				
				break;
		}
	};
	
	MapLayersMediator.prototype.initMapList = function()
	{
		var container = this.getViewComponent();
		var list = document.getElementById("mapsList");
		var addButton = document.getElementById("addMapButton");
		var removeButton = document.getElementById("removeMapButton");
		
		this._screen.init( list, addButton, removeButton );
		this._screen.signal.addListener( MapLayersScreen.ON_SELECT, this.selectMapHandler, this );
		this._screen.signal.addListener( MapLayersScreen.ADD_MAP, this.addMapHandler, this );
		this._screen.signal.addListener( MapLayersScreen.REMOVE_MAP, this.removeMapHandler, this );
		
		this._screen.clean();
	};
	
	MapLayersMediator.prototype.selectMapHandler = function(p_id)
	{
		this.getFacade().sendNotification( ApplicationNotification.SELECT_MAP, p_id );
	};
	
	MapLayersMediator.prototype.addMapHandler = function(p_id)
	{
		this.getFacade().sendNotification( ApplicationNotification.ADD_MAP, p_id );
	};
	
	MapLayersMediator.prototype.removeMapHandler = function()
	{
		this.getFacade().sendNotification( ApplicationNotification.REMOVE_MAP );
	};
	
	
	
	